import {
  createAddMarkInfoStatement,
  videoRealMarkListByVedioIdStatement,
  videoMarkListByVedioIdStatement,
  videoMarkDeleteStatement,
  getMaxId,
} from './statements';
import { getDB } from '../../db';

/**
 * 添加视频标注 支持批量添加
 * @param markInfoInfos 视频标注信息
 */
export const addMarkInfo = (markInfoInfos: LX.DBService.MarkInfoInfo[]) => {
  const insertStatement = createAddMarkInfoStatement();
  const db = getDB();
  db.transaction((markInfoInfos: LX.DBService.MarkInfoInfo[]) => {
    for (const markInfoInfo of markInfoInfos) {
      insertStatement.run({
        id: markInfoInfo.id,
        videoInfoId: markInfoInfo.videoInfoId,
        startTime: markInfoInfo.startTime,
        endTime: markInfoInfo.endTime,
        label: markInfoInfo.label,
        describe: markInfoInfo.describe,
        channel: markInfoInfo.channel,
        status: markInfoInfo.status,
        createTime: markInfoInfo.createTime,
        createUser: markInfoInfo.createUser,
      });
    }
  })(markInfoInfos);
};

//导出标注列表
export const getRightMarkList = (id: String) => {
  const statements = videoRealMarkListByVedioIdStatement(); //实时标注
  const statements2 = videoMarkListByVedioIdStatement(); //多路视频标注
  return statements.all(id).concat(statements2.all(id));
};

/**
 * 时间标记与标注   导出标注列表/多路视频标注 右侧列表 [标注]
 */
export const getVideoMarkListByVedioIdStatement = (id: String) => {
  const statements = videoMarkListByVedioIdStatement();
  return statements.all(id);
};

/**
 * 删除视频标注
 */
export const deleteMarkItem = (data: LX.DBService.VideoMarkUpdate) => {
  const statements = videoMarkDeleteStatement();
  const db = getDB();
  db.transaction((data) => {
    statements.run(data);
  })(data);
};

/**
 * 添加视频标注 支持批量添加
 * @param markInfoInfos 视频标注信息
 */
export const addMarkItem = (markInfoInfos: LX.DBService.MarkInfoInfo[]) => {
  const insertStatement = createAddMarkInfoStatement();
  const db = getDB();
  const getMaxIdStatment = getMaxId();
  let rest = getMaxIdStatment.get();
  let maxId = '0';
  if (rest?.id !== null) {
    maxId = rest ? rest.id : '0';
  }
  db.transaction((markInfoInfos: LX.DBService.MarkInfoInfo[]) => {
    for (const markInfoInfo of markInfoInfos) {
      maxId = (parseInt(maxId) + 1).toString();
      insertStatement.run({
        id: maxId,
        videoInfoId: markInfoInfo.videoInfoId,
        startTime: markInfoInfo.startTime,
        endTime: markInfoInfo.endTime,
        label: markInfoInfo.label,
        describe: markInfoInfo.describe,
        channel: markInfoInfo.channel,
        status: markInfoInfo.status,
        createTime: markInfoInfo.createTime,
        createUser: markInfoInfo.createUser,
      });
    }
  })(markInfoInfos);
};
